// Clean results of robustness check simulations
// Author Levi Marks levi.marks.1>at>gmail.com

keep ag_* mc_*
keep if _n == 1

gen dummy = 1
reshape long ag_cost_ ag_reduction_ ag_value_ mc_, i(dummy) j(tax)
drop dummy

rename tax tax_mcf
rename mc_ mc_mcf
rename ag_reduction_ ag_reduction_mcf
rename ag_cost_ ag_cost
rename ag_value_ ag_value
gen marginal_value = 3.123 // Calculated this by setting the step size to .001. Using this for the graph to create a straight line for readability; however, point estimates in the paper are based on the "ag_value" variable, which will vary slightly from the average depending on which facilities reach the lower bound first.

replace mc_mcf = marginal_value if tax_mcf == 0

replace tax_mcf = tax_mcf*$step_size
recast double tax_mcf
replace tax_mcf = round(tax_mcf,.01)
gen tax_tco2 = (tax*53.68)/34
gen ag_reduction_tco2e = ag_reduction_mcf*34/53.68
gen mc_tco2e = mc_mcf*53.68/34
di total_emissions
gen reduction_percent = (ag_reduction_mcf/total_emissions)*100

gen net_cost = (ag_cost - ag_value)/28400000000

replace ag_reduction_mcf = 0 if missing(ag_reduction_mcf)
replace ag_reduction_tco2e = 0 if missing(ag_reduction_tco2e)

format mc* tax* %6.2fc
format ag* %12.0fc
format reduction_percent %6.1fc

order tax_mcf tax_tco2 mc_mcf mc_tco2e ag_cost ag_reduction_mcf ag_reduction_tco2e reduction_percent

// Interpolating $5, $20, and $42 carbon prices
set obs `=_N+1'
replace tax_tco2 = 5 if missing(tax_tco2)
set obs `=_N+1'
replace tax_tco2 = 20 if missing(tax_tco2)
set obs `=_N+1'
replace tax_tco2 = 44.4 if missing(tax_tco2)
sort tax_tco2
gen percent_next = (tax_tco2 - tax_tco2[_n-1])/(tax_tco2[_n+1] - tax_tco2[_n-1]) if missing(mc_mcf)
gen percent_prev = (tax_tco2[_n+1] - tax_tco2)/(tax_tco2[_n+1] - tax_tco2[_n-1]) if missing(mc_mcf)
replace tax_mcf = tax_mcf[_n-1]*percent_prev + tax_mcf[_n+1]*percent_next if missing(tax_mcf)
replace tax_tco2 = tax_tco2[_n-1]*percent_prev + tax_tco2[_n+1]*percent_next if missing(tax_tco2)
foreach var in mc_mcf mc_tco2e ag_cost ag_reduction_mcf ag_reduction_tco2e reduction_percent ag_value net_cost {
	replace `var' = `var'[_n-1]*percent_prev + `var'[_n+1]*percent_next if missing(`var')
}

gen avg_cost_tco2e = (ag_cost - ag_value)/ag_reduction_tco2e
format avg_cost_tco2e %6.2fc
format net_cost %6.4fc

recast double tax_tco2
replace tax_tco2 = 44.4 if tax_tco2 > 44.39 & tax_tco2 < 44.41 // Became 44.400002 due to some variable formatting bug

gen double ag_reduction_tco2e_round = round(ag_reduction_tco2e,1000)
format ag_reduction_tco2e_round %15.0fc
drop ag_reduction_tco2e
rename ag_reduction_tco2e_round ag_reduction_tco2e

gen total_net_cost = (ag_cost - ag_value)/1000000
gen climate_benefit = (ag_reduction_tco2e*44.4)/1000000
gen welfare = climate_benefit - total_net_cost

format climate_benefit welfare total_net_cost %10.0fc

replace ag_cost = ag_cost/1000000
replace ag_value = ag_value/1000000
